package com.mazha.rabbitmq.controller;

import com.alibaba.fastjson.JSON;
import com.mazha.rabbitmq.pojo.domain.Book;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 推送消息控制器
 *
 * @author leizi
 * @create 2023-12-03 22:18
 */
@RestController
@Slf4j
@RequestMapping("/producer")
public class ProducerController {

    private final RabbitTemplate rabbitTemplate;

    public ProducerController(RabbitTemplate rabbitTemplate) {
        this.rabbitTemplate = rabbitTemplate;
    }

    @PostMapping("/testSend")
    public void testSendMessage(@RequestBody Book book) {
        log.info("生产者发送消息:{}", JSON.toJSONString(book));
        /*
         * 对象被默认序列化以后发送出去
         * object默认当成消息体，只需要传入要发送的对象，自动序列化发送给rabbitmq
         * */
        rabbitTemplate.convertAndSend("chat.fanout.exchange", "chat.room.key", book);
    }
}
